//
//  TuijianCaiyouLikeCollectionViewCell.swift
//  xmly
//
//  Created by 刘文超 on 2020/1/8.
//  Copyright © 2020 刘文超. All rights reserved.
//

import UIKit

protocol TuijianCaiyouLikeCollectionViewCellDelegate :NSObjectProtocol{
    func TuijianItemClick(indexPathRowDict:NSDictionary)
}

class TuijianCaiyouLikeCollectionViewCell: UICollectionViewCell {
    weak  var delegate : TuijianCaiyouLikeCollectionViewCellDelegate?
    let flayoutWidth = (ScreenWidth-20*2-20)/3
    
    
    lazy var collectionView: UICollectionView = {
        let flayout = UICollectionViewFlowLayout.init();
        flayout.minimumLineSpacing = 5
        flayout.minimumInteritemSpacing = 10
        flayout.sectionInset = .init(top: 0, left: 20, bottom: 0, right: 20)
        flayout.itemSize = CGSize.init(width: flayoutWidth, height: (flayoutWidth*1.2+25+30+10))
        let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: flayout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.white
        collectionView.isScrollEnabled = false
        collectionView.register(itemCell.self, forCellWithReuseIdentifier: "cell")
        return collectionView
    }()
    
    
    lazy var changeButton:UIButton = {
        let button = UIButton.init()
        button.layer.cornerRadius = 5
        button.layer.masksToBounds = true
        button.layer.borderColor = UIColor.orange.cgColor
        button.layer.borderWidth = 1
        button.setTitleColor(UIColor.red, for: .normal)
        button.setTitle("换一换", for: .normal)
        return button
    }()
    
    
    
    let items = [["image":"http://g.hiphotos.baidu.com/baike/pic/item/c2fdfc039245d688d21b38feadc27d1ed21b244a.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"],["image":"http://img10.360buyimg.com/popWaterMark/jfs/t160/195/716544613/62884/83cd31d/5396067eNa57e536f.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"],["image":"http://g.hiphotos.baidu.com/baike/pic/item/c2fdfc039245d688d21b38feadc27d1ed21b244a.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"],["image":"http://g.hiphotos.baidu.com/baike/pic/item/c2fdfc039245d688d21b38feadc27d1ed21b244a.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"],["image":"http://img3.tbcdn.cn/tfscom/i1/1974190653/TB2dnpvmXXXXXabXXXXXXXXXXXX_%21%211974190653.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"],["image":"http://g.hiphotos.baidu.com/baike/pic/item/c2fdfc039245d688d21b38feadc27d1ed21b244a.jpg","title":"这是一个大标题啊非常大","content":"这是一段文字介绍，关于书本知识的简介"]]
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo((flayoutWidth*1.2+25+30+10)*2)
            make.top.equalTo(10)
        }
        
        self.addSubview(self.changeButton)
        self.changeButton.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-10)
            make.height.equalTo(30)
            make.width.equalTo(80)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension TuijianCaiyouLikeCollectionViewCell:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:itemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! itemCell
        print(items[indexPath.row])
        cell.setDatainfo(dict: items[indexPath.row] as NSDictionary)
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let dict:NSDictionary = items[indexPath.item] as NSDictionary
        self.delegate?.TuijianItemClick(indexPathRowDict: dict)
    }
}


class itemCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    let flayoutWidth = (ScreenWidth-20*2-20)/3
    
    
    lazy var lwcimageView:UIImageView = {
        let imageview = UIImageView.init()
        imageview.contentMode = .scaleAspectFit
        imageview.image = UIImage.init(named: "fj")
        return imageview
    }()
    
    lazy var titleLabel:UILabel = {
        let label = UILabel.init()
        label.font = .systemFont(ofSize: 16)
        label.text = "如何成为一个高情商的人？"
        return label
    }()
    
    
    lazy var contentLabel:UILabel = {
        let label = UILabel.init()
        label.font = .systemFont(ofSize: 13)
        label.textColor = labelGrayColor
        label.numberOfLines = 2
        label.text = "这是一段文字介绍，关于书本知识的简介"
        return label
    }()
    
    
    func layoutUI() {
        self.addSubview(lwcimageView)
        lwcimageView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(flayoutWidth)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(lwcimageView.snp.bottom).offset(5)
            make.left.right.equalToSuperview()
            make.height.equalTo(25)
        }
        
        self.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(5)
        }
        
    }
    
    
    func setDatainfo(dict:NSDictionary) {
        lwcimageView.kf.setImage(with: URL.init(string: dict["image"] as? String ?? ""))
        titleLabel.text = dict["title"] as? String
        contentLabel.text = dict["content"] as? String
    }
}
